package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionNotice;
import com.yanqu.road.logic.bussiness.union.cross.CrossUnionBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.LocalDateTimeHelper;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CrossUnionNoticeMgr extends TempMgr {

    private static Object lockObj = new Object();

    private static Map<String, LinkedList<CrossUnionNotice>> unionNoticeMap;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadData() {
        unionNoticeMap = CrossUnionBussiness.getUnionNoticeMap(LocalDateTimeHelper.getZeroTimeTimeStamp() / 1000, GameConfig.UNION_NOTICE_MAX_COUNT);
        return true;
    }

    @Override
    public boolean save() {
        if(null != unionNoticeMap){
            for(List<CrossUnionNotice> dataList : unionNoticeMap.values()){
                List<CrossUnionNotice> tempList;
                synchronized (dataList) {
                    tempList = new ArrayList<>(dataList);
                }
                for(CrossUnionNotice unionNotice : tempList){
                    if(unionNotice.isInsertOption()){
                        CrossUnionBussiness.addUnionNotice(unionNotice);
                    }
                }
            }
        }
        return true;
    }

    public static void addNotice(CrossUserBaseInfo player, String unionUid, int noticeType, Object... values) {
        CrossUnionNotice notice = initUnionNotice(player, unionUid, noticeType, values);
        if (!unionNoticeMap.containsKey(unionUid)) {
            checkUnionNotice(unionUid);
        }
        LinkedList<CrossUnionNotice> dataList = unionNoticeMap.get(unionUid);
        synchronized (dataList) {
            if(dataList.size() >= GameConfig.UNION_NOTICE_MAX_COUNT) {
                dataList.removeFirst();
            }
            dataList.addLast(notice);
        }
    }

    private static CrossUnionNotice initUnionNotice(CrossUserBaseInfo player, String unionUid, int noticeType, Object[] values) {
        CrossUnionNotice notice = new CrossUnionNotice();
        notice.setType(noticeType);
        notice.setUnionUid(unionUid);
        if(null != player) {
            notice.setUserId(player.getUserId());
            notice.setNickName(player.getUserBaseInfo().getNickName());
        }else {
            notice.setUserId(0);
            notice.setNickName("");
        }
        notice.setNoticeTime(System.currentTimeMillis() / 1000);
        notice.setContent(encodeContent(values));
        notice.setInsertOption();
        return notice;
    }

    public static final String SEPARATOR = "&#!";

    public static String encodeContent(Object ... strings) {
        StringBuilder content = new StringBuilder();
        if (strings != null) {
            for (Object str : strings) {
                content.append(str).append(SEPARATOR);
            }
        }
        if(content.length() > 0){
            return content.substring(0, content.length() - SEPARATOR.length());
        }
        return content.toString();
    }

    public static void checkUnionNotice(String unionUid) {
        synchronized (lockObj) {
            if(!unionNoticeMap.containsKey(unionUid)) {
                unionNoticeMap.put(unionUid, new LinkedList<>());
            }
        }
    }

    public static List<CrossUnionNotice> getUnionNoticeList(String unionUid){
        return unionNoticeMap.get(unionUid);
    }
}
